package com.yenlien.traceyun.control.service;

import com.yenlien.traceyun.control.dao.GoodsDao;
import com.yenlien.traceyun.control.dao.PlantDao;
import com.yenlien.traceyun.control.entity.Goods;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jialong
 * @date 2021/05/19
 * @description: 货物的业务层
 **/

@Service
public class GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private PlantDao plantDao;


    @Transactional
    public Goods save(Goods goods) {
        // 校验种植批次号是否存在
        String plantBatch = goods.getPlantBatch();
        if (StringUtils.isNotBlank(plantBatch)) {
            String byBatch = plantDao.findByBatch(plantBatch);
//            JSONObject json = restService.getJson("http://traceyun-plant/plant/checkBatch/" + plantBatch);
            if (byBatch == null) {
                throw new BusinessException("该种植批次不存在");
            }
        }
        return goodsDao.save(goods);
    }


    public Page<Goods> findList(Goods goods) {
        Pageable pageable = PageRequest.of(goods.getPage(), goods.getPageSize(), Sort.Direction.DESC, ("createTime"));
        return goodsDao.findAll((Root<Goods> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            //构造查询条件
            List<Predicate> list = new ArrayList<>();
//            if(StringUtils.isNotBlank(shelves.getGoodsName())){
//                list.add(cb.equal(root.get("goodsName"), "%" + shelves.getGoodsName() + "%"));
//            }
//            if(StringUtils.isNotBlank(shelves.getGoodsNum())){
//                list.add(cb.equal(root.get("goodsNum"), shelves.getGoodsNum()));
//            }
//            if(StringUtils.isNotBlank(shelves.getGoodsValue())){
//                list.add(cb.equal(root.get("goodsValue"), shelves.getGoodsValue()));
//            }
//            if(StringUtils.isNotBlank(shelves.getGoodsLevel())){
//                list.add(cb.equal(root.get("goodsLevel"), shelves.getGoodsLevel()));
//            }
//            if(StringUtils.isNotBlank(shelves.getOwnerName())){
//                list.add(cb.equal(root.get("ownerName"), shelves.getOwnerName()));
//            }
//            if(StringUtils.isNotBlank(shelves.getType())){
//                list.add(cb.equal(root.get("type"), shelves.getType()));
//            }
//            if(StringUtils.isNotBlank(shelves.getBatch())){
//                list.add(cb.equal(root.get("batch"), shelves.getBatch()));
//            }
//            if(StringUtils.isNotBlank(shelves.getExpiration())){
//                list.add(cb.equal(root.get("expiration"), shelves.getExpiration()));
//            }
            String companyId = UserAuthUtil.getLoginUser().getCompanyId();
            list.add(cb.equal(root.get("companyId"), companyId));
            Predicate[] predicate = new Predicate[list.size()];
            return cb.and(list.toArray(predicate));
        }, pageable);
    }


    public Goods findById(Long id) {
        return goodsDao.findById(id).get();
    }

    public Goods findBySId(Long id) {
        return goodsDao.findBySId(id);
    }


    public void del(Long goodsId) {
        goodsDao.deleteById(goodsId);
    }


}
